//修改操作记录--撤销交易记录：/change-operate-position
import http from 'k6/http';
import { check } from 'k6';
import { URL } from './config.js';

// 1. 压测场景：10 个虚拟用户，持续 30 秒
export const options = {
    //iteations字段：限制总迭代次数，运行20次循环结束，如果不执行，则是不限制迭代次数，时间到就结束
    iterations: 20,//循环20次结束
  vus: 10,//10个并发
  duration: '30s',
};

// 2. 常量
//const BASE = 'https://dev.xiaobeiyangji.com/yangji-api';
const BASE = `${URL}/yangji-api`;

export default function () {
  // 登录 → 拿 accessToken: https://dev.xiaobeiyangji.com/api/login/phone
 const loginRes = http.post(`${BASE}/api/login/phone`,JSON.stringify({
       phone: '15298055260',
       code: '666666',
       clientType: 'PHONE'
     }),
     { headers: { 'Content-Type': 'application/json' } }
   );
  // https://api.xiaobeiyangji.com/api/change-operate-position
  const payload=JSON.stringify(
{
    "isEdit":true,
    "_id":"3270a33a-a022-41a0-b2ef-ed38acbc7684",
    "type":"buy",
    "unionId":"o896o5_scUqyJKkRTR1z9Wbm4V98",
    "code":"012863",
    "accountId":"081146b5-a12f-4fea-97d3-d165fca7a49a",
    "version":"3.4.0.X"}
  );

  // 简单断言
  check(loginRes, { 'login 200': r => r.status === 200 });

  // 提取 token（返回体在 data.accessToken）
  const token = loginRes.json('data.accessToken');

  // ② 用 token 调 /api/get-version
  const verRes = http.post(`${BASE}/api/change-operate-position`,payload,{ headers: {'Content-Type': 'application/json', Authorization: `Bearer ${token}` } }
  );
  console.log('状态码',verRes.status);
  console.log('返回值',verRes.body);
}